/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import entity.Product;
import entity.SaleOrder;
import entity.SaleOrderDetail;
import java.util.List;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.ProductServiceLocal;
import service.SaleOrderDetailServiceLocal;
import service.SaleOrderServiceLocal;

/**
 *
 * @author SONY
 */
public class UpdateStatusStrutsAction extends org.apache.struts.action.Action {
    
    @EJB SaleOrderServiceLocal saleOrderServiceLocal;
    @EJB SaleOrderDetailServiceLocal saleOrderDetailServiceLocal;
    @EJB ProductServiceLocal productServiceLocal;
    
    private UserTransaction ut;

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        try {
            ServletContext context = servlet.getServletContext();
            saleOrderServiceLocal = (SaleOrderServiceLocal) context.getAttribute("saleOrderServiceLocal");
            int oid = Integer.parseInt(request.getParameter("oid").toString());
            String status = request.getParameter("status_update");
            SaleOrder saleorder = saleOrderServiceLocal.getSaleOrder_Details_bySaleOrderID(oid);
            if(saleorder != null) {
                if(saleorder.getStatus().equals("Hủy")) {
                    request.setAttribute("notify", "Không thể cập nhật trạng thái đơn hàng cho những đơn hàng đã bị hủy.");
                    return mapping.findForward(SUCCESS);
                } else {
                    Context ctx = new InitialContext();
                    ut = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
                    ut.begin(); //start the transaction
                    if(saleOrderServiceLocal.updateStatus_bySaleOrderID(oid, status)) {
                        if(status.equals("Hủy")) {
                            saleOrderDetailServiceLocal = (SaleOrderDetailServiceLocal) context.getAttribute("saleOrderDetailServiceLocal");
                            List<SaleOrderDetail> lstSaleOrderDetails = saleOrderDetailServiceLocal.getAllSaleOrderDetails_bySaleOrderID(oid);
                            for(int j = 0; j < lstSaleOrderDetails.size(); j++) {
                                String ProductID = lstSaleOrderDetails.get(j).getProductID().getProductID();
                                int OrderQty = lstSaleOrderDetails.get(j).getOrderQty();
                                //cập nhật lại sl hàng tồn kho khi trạng thái đặt hàng bị hủy bỏ
                                productServiceLocal = (ProductServiceLocal) context.getAttribute("productServiceLocal");
                                Product product = productServiceLocal.getProductDetails_byPID(ProductID);
                                int QOH = product.getQoh();
                                int QOH_new = QOH + OrderQty;
                                if(productServiceLocal.updateQOH_byPID(ProductID, QOH_new)) {
                                    System.out.println("updated successful!");
                                }
                            }
                        }
                        //System.out.println("changed Status successful!");
                        ut.commit(); 
                        request.setAttribute("notify", "Cập Nhật Trạng Thái Đơn Hàng Thành Công!");
                        return mapping.findForward(SUCCESS);
                    }                       
                }
            } else {
                request.setAttribute("notify", "Mã Đơn Hàng này không tồn tại! Hãy kiểm tra lại.");
                return mapping.findForward(SUCCESS);                    
            }
        } catch(Exception e) {
            System.err.println(e);
            ut.rollback();
        }
        request.setAttribute("notify", "Cập Nhật Trạng Thái Đơn Hàng Thất Bại Do Bị Gián Đoạn. Hãy Thực Hiện Lại Sau!");
        return mapping.findForward(SUCCESS);
    }
}
